<%@ page language="java" contentType="text/html; charset=UTF-8" pageEncoding="UTF-8"%>
<!DOCTYPE html>
<html>
<head>
<%@ include file="/web/header.jsp"%>
<title>Insert title here</title>
</head>
<body>
	<div class="layui-anim layui-anim-scale">
	<fieldset class="layui-elem-field layui-field-title">
		<legend>信息维护</legend>
	<!-- 一个form表格 在此表格-->
	<from class="layui-form" action=""> <!-- 允许排在一行  <div class="layui-input-block"></div> 不排在一行 -->
	<div class="layui-input-inline">
		<!-- 给定一个layui的lable样式 -->
		<label class="layui-form-label">用户名：</label>
		<div class="layui-input-block">
			<!-- 自己排在一行 -->
			<input type="text" id="bianhao" required lay-verify="required" placeholder="用户名" autocomplete="off" class="layui-input">
		</div>
	</div>
	<div class="layui-input-inline">
		<!-- 给定一个layui的lable样式 -->
		<label class="layui-form-label">姓名：</label>
		<div class="layui-input-block">
			<!-- 自己排在一行 -->
			<input type="text" id="name" required lay-verify="required" placeholder="姓名" autocomplete="off" class="layui-input">
		</div>
	</div>
	<div class="layui-input-inline">
		&nbsp;&nbsp;
		<input type="button" class="layui-btn" onclick="openSelect()" value="查询">
		<input type="button" class="layui-btn" onclick="openAdd()" value="添加">
		<input type="hidden" id = "pageIndex" value="1"/>
		<input type="hidden" id = "pageLimit" value="10"/>
	</div>
	</from>
	</fieldset>
	<table class="layui-table">
		<colgroup>
			<col width="150">
			<col width="200">
			<col>
		</colgroup>
		<thead>
			<tr>
				<th>编号</th>
				<th>用户名</th>
				<th>姓名</th>
				<th>所属部门</th>
				<th>部门名称</th>
				<th>操作</th>
			</tr>
		</thead>
		<tbody id="tbody">
			<tr>
			</tr>
		</tbody>
	</table>
	</div>
	<div id="pageInfoUser" style="text-align: right;padding-right: 30px"></div>
	<script>
		openSelect();
		layui.use('form', function() {
			var form = layui.form; //只有执行了这一步，部分表单元素才会自动修饰成功
			form.render();
		});
		function openSelect() {
			var bianhao = $("#bianhao").val();
			var name = $("#name").val();
			var pageIndex = $("#pageIndex").val();
			var pageLimit = $("#pageLimit").val();
			$.ajax({
				url : "/work/EmployeeServlet",
				data : {
					action : "page",
					bianhao : bianhao,
					name : name,
					pageIndex : pageIndex,
					pageLimit : pageLimit,
					},
				type : "post",
				dataType : "json",
				success : function(data) {
					var curr =  $("#pageIndex").val();
					var limit = $("#pageLimit").val();
					setPageInfo('pageInfoUser', data.count, curr, limit, function(obj, first){
						console.log(obj.curr)
						var pageIndex = $("#pageIndex").val(obj.curr);
						var pageLimit = $("#pageLimit").val(obj.limit);
// 						$("input[name='pageIndex']").val(obj.curr);
// 						$("input[name='pageLimit']").val(obj.limit);
						if(!first){openSelect();}//首次不执行
					})
					$("#tbody").html("");
					var html = "";
					console.log(pageIndex)
					console.log(pageLimit)
					var num = pageIndex * pageLimit - 9;
					$.each(data.data,function(i, dom) {
						var id = dom.id ? dom.id : '';
						var name = dom.name ? dom.name: '';
						var password = dom.password ? dom.password: '';
						var d_id = dom.d_id ? dom.d_id: '';
						var d_name = dom.d_name ? dom.d_name: '';
						var temp = "<tr id = 'tr" + i + "'>"
						+ "<td id = 'tda" + i + "'>"+ num + "</td>"
						+ "<td id = 'tdb" + i + "'>"+ id + "</td>"
						+ "<td id = 'tdc" + i + "'>"+ name+ "</td>"
						+ "<td id = 'tdf" + i + "'>"+ d_id+ "</td>"
						+ "<td id = 'tdf" + i + "'>"+ d_name+ "</td>"
						+ "<td id = 'tdg" + i + "'>"
						+ "<button type='button' class='layui-btn' onclick='openDialog("+ id+ ")'>修改</button>"
						+ "<button type='button' class='layui-btn' onclick='testdel("+ id + ")'>删除</button>"
						+ "<button type='button' class='layui-btn' onclick='upPass("+ id + ")'>修改密码</button>"
						+ "<button type='button' class='layui-btn' onclick='chongPass("+ id + ")'>重置密码</button>"
						+ "<button type='button' class='layui-btn' onclick='upDepart("+ id + ")'>修改部门</button>"
						+ "</tr>";
						num++;
						$("#tbody").append(temp);
						});
					}
					})
		}
// 		function openSelect() {
// 			var bianhao = $("#bianhao").val();
// 			var name = $("#name").val();
// 			$.ajax({
// 				url : "/work/EmployeeServlet",
// 				data : {
// 					action : "select",
// 					bianhao : bianhao,
// 					name : name,
// 					},
// 				type : "post",
// 				dataType : "json",
// 				success : function(data) {
// 					console.log(data)
// 					$("#tbody").html("");
// 					var html = "";
// 					$.each(data,function(i, dom) {
// 						var id = dom.id ? dom.id : '';
// 						var name = dom.name ? dom.name: '';
// 						var password = dom.password ? dom.password: '';
// 						var sex = dom.sex ? dom.sex: '';
// 						var date = dom.date ? dom.date: '';
// 						var d_id = dom.d_id ? dom.d_id: '';
// 						var temp = "<tr id = 'tr" + i + "'>"
// 						+ "<td id = 'tda" + i + "'>"+ i + "</td>"
// 						+ "<td id = 'tdb" + i + "'>"+ id + "</td>"
// 						+ "<td id = 'tdb" + i + "'>" + password + "</td>"
// 						+ "<td id = 'tdc" + i + "'>"+ name+ "</td>"
// 						+ "<td id = 'tdd" + i + "'>"+ sex+ "</td>"
// 						+ "<td id = 'tde" + i + "'>"+ date+ "</td>"
// 						+ "<td id = 'tdf" + i + "'>"+ d_id+ "</td>"
// 						+ "<td id = 'tdg" + i + "'>"
// 						+ "<button type='button' class='layui-btn' onclick='openDialog("+ id+ ")'>修改</button>"
// 						+ "<button type='button' class='layui-btn' onclick='testdel("+ id+ ")'>删除</button>"
// 						+ "</tr>";
// 						$("#tbody").append(temp);
// 						});
// 					}
// 					})
// 					}
// 		function ppp() {
// 			$.ajax({
// 				url : "/work/EmployeeServlet",
// 				data : {
// 					action : "huixian",
// 					},
// 				type : "post",
// 				dataType : "json",
// 				success : function(data) {
// 					$("#tbody").html("");
// 					var html = "";
// 					$.each(data,function(i, dom) {
// 						var id = dom.id ? dom.id : '';
// 						var name = dom.name ? dom.name: '';
// 						var password = dom.password ? dom.password: '';
// 						var sex = dom.sex ? dom.sex: '';
// 						var date = dom.date ? dom.date: '';
// 						var d_id = dom.d_id ? dom.d_id: '';
// 						var temp = "<tr id = 'tr" + id + "'>"
// 						+ "<td id = 'tda" + i + "'>"+ i + "</td>"
// 						+ "<td id = 'tdb" + i + "'>"+ id + "</td>"
// 						+ "<td id = 'tdb" + i + "'>" + password + "</td>"
// 						+ "<td id = 'tdc" + i + "'>"+ name+ "</td>"
// 						+ "<td id = 'tdd" + i + "'>"+ sex+ "</td>"
// 						+ "<td id = 'tde" + i + "'>"+ date+ "</td>"
// 						+ "<td id = 'tdf" + i + "'>"+ d_id+ "</td>"
// 						+ "<td id = 'tdg" + i + "'>"
// 						+ "<button type='button' class='layui-btn' onclick='openDialog("+ id+ ")'>修改</button>"
// 						+ "<button type='button' class='layui-btn' onclick='testdel("+ id+ ")'>删除</button>"
// 						+ "</tr>";
// 						$("#tbody").append(temp);
// 						});
// 					}
// 					})
// 					}

		function testdel(id) {
			if (confirm("是否要删除数据？")) {
				$.ajax({
					url : "/work/EmployeeServlet",
					data : {
						action : "del",
						id : id,
					},
					type : "post",
					dataType : "text",
					success : function(data) {
						layer.msg("删除成功~")
						refresh();
					}
				})
			}
		}
		function openAdd(){
			openLayer('/web/page/employee/add.jsp',refresh)
		}
		function refresh(){
			$("#tbody").html("");
			openSelect();
		}
		function upPass(id){
			openLayer('/web/page/employee/upPass.jsp?code='+id,refresh)
		}
		function chongPass(id){
			$.ajax({
				url : "/work/EmployeeServlet",
				data : {
					action : "udpass",
					code : id,
					pass : "123456",
				},
				type : "post",
				dataType : "text",
				success : function(data) {
					layer.msg("重置成功~")
					refresh();
				}
			})
		}
		
		function upDepart(id){
			openLayer( '/web/page/employee/upDepart.jsp?code='+id, refresh)
		}
		function openDialog(id){
			openLayer( '/web/page/employee/upd.jsp?code='+id, refresh)
		}
	</script>
</body>
</html>